<?php

namespace App\Http\Controllers\Api\SmallApp;

use App\Http\Controllers\Common\ApiController;
use App\Models\Car\CarInfo;
use App\Models\Car\FixCarOrder;
use App\Models\Car\RentUnit;
use App\Models\Constant\CarConstant;
use App\Models\User\User;
use Illuminate\Http\Request;

class UserController extends ApiController{

    public function __construct() {
        $this->instance = new User();
    }

    public function login(Request $request){
        $phone = $request->input('phone');
        $password = $request->input('password');

        $this->validate($request, [
            'phone'=>['required','regex:/^1[345789][0-9]{9}$/'],
            'password'=>'required'
        ]);

        $user = User::query()->where(['phone'=>$phone])->first();
        if(empty($user))
            return responseFail('未查找到用户');
        else if($user->password_md5!=md5($password)) {
            return responseFail('密码错误！');
        }else{
            $rentUnit = RentUnit::find($user->rent_unit_id);
            $user->rent_unit_name = empty($rentUnit)?'':$rentUnit->unit_name;
            return responseSuccess($user->toArray());
        }
    }

    public function addFixCarOrder(Request $request){
        $this->validate($request, [
            'user_id'=>'required|exists:user,id',
            'car_no'=>'required|exists:car_info,car_no',
            'question_desc'=>'required',
            'people'=>'required',
            'type'=>'required',
            'imgs'=>'required',
        ],[],[
            'user_id'=>'用户',
            'car_no'=>'车牌号',
            'question_desc'=>'问题描述',
            'imgs'=>'图片',
            'people'=>'联系人',
            'type'=>'维修类型',
        ]);

        $user_id = $request->input('user_id');
        $car_no = $request->input('car_no');
        $question_desc = $request->input('question_desc');
        $beizhu = $request->input('beizhu');
        $imgs = $request->input('imgs');
        $people = $request->input('people');
        $type = $request->input('type');
        try {
            if(is_array($imgs)){
                $imgs = implode(',', $imgs);
            }
            $user = User::query()->where(['id'=>$user_id])->with('getRentUnit')->first();

            $records = FixCarOrder::query()->updateOrCreate(compact('user_id','car_no'),[
                'user_id'=>$user_id,
                'car_no'=>$car_no,
                'question_desc'=>$question_desc,
                'beizhu'=>$beizhu,
                'imgs'=>$imgs,
                'people'=>$people,
                'type'=>$type,
                'rent_unit_id'=>$user->rent_unit_id,
                'rent_unit_name'=>$user->getRentUnit->unit_name??'未知',
            ]);

            //车辆信息更换成待维修
            CarInfo::query()->where('car_no',$car_no)->update(['status'=>CarConstant::CAR_STATUS_FIXING]);

            return responseSuccess($records->toArray());
        }catch (\Exception $exception){
            return responseFail($exception->getMessage());
        }
    }

    public function detail(Request $request){
        $id = $request->input('id',0);
        $record = $this->instance->getRecordById($id);
        return responseSuccess($record->toArray(),1);
    }
}
